package ctgu.yao.cloud.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import ctgu.yao.cloud.dto.PageDTO;
import ctgu.yao.cloud.dto.ResultDTO;
import ctgu.yao.cloud.form.SearchForm;
import ctgu.yao.cloud.po.Recruitment;
import ctgu.yao.cloud.service.RecruitmentService;
import ctgu.yao.cloud.util.ResultDTOUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Y
 * @since 2022-08-16
 */
@RestController
@ResponseBody
@RequestMapping("/recruitment")
public class RecruitmentController {
    //发布职位信息
    @Autowired
    private RecruitmentService recruitmentService;
    @PostMapping("/publish/{cuid}")
    public ResultDTO publish(@RequestBody Recruitment recruitment,@PathVariable("cuid")Integer cuid){
        this.recruitmentService.save(recruitment,cuid);
        return ResultDTOUtil.success(null);
    }
    //查看职位(分页查看部分数据）
    @GetMapping("list/{page}/{size}/{cuid}")
    public ResultDTO list(@PathVariable("page")Integer page,@PathVariable("size")Integer size,@PathVariable("cuid")Integer cuid){
        PageDTO pageDTO =this.recruitmentService.list(page,size,cuid);
        return ResultDTOUtil.success(pageDTO);
    }
    //分页查看所有数据所有数据
    @GetMapping("list/{page}/{size}")
    public ResultDTO list(@PathVariable("page")Integer page,@PathVariable("size")Integer size){
        PageDTO pageDTO =this.recruitmentService.list(page,size);
        return ResultDTOUtil.success(pageDTO);
    }
    //职位信息修改
    @GetMapping("/findById/{reid}")
    public ResultDTO findByID(@PathVariable("reid") Integer reid){
            Recruitment recruitment = this.recruitmentService.getById(reid);
            return ResultDTOUtil.success(recruitment);
    }
    @PutMapping("/update")
    public ResultDTO update(@RequestBody Recruitment recruitment){
            boolean update=this.recruitmentService.updateById(recruitment);
            if(!update) return ResultDTOUtil.fail();
            return ResultDTOUtil.success(null);
    }
    //职位信息删除
    @DeleteMapping("/deleteById/{id}")
    public ResultDTO deleteById(@PathVariable("id") Integer id){
        boolean delete=this.recruitmentService.removeById(id);
        if(!delete) return ResultDTOUtil.fail();
        return ResultDTOUtil.success(null);
    }


    //按条件查询
    @GetMapping("/search")
    public ResultDTO search(SearchForm searchForm){
        return ResultDTOUtil.success(this.recruitmentService.search(searchForm));
    }

    //申请
    @RequestMapping("/application/{reid}/{rid}")
    public ResultDTO application(@PathVariable("reid") Integer reid,@PathVariable("rid") Integer rid){
        Boolean application =this.recruitmentService.application(reid, rid);
        if(!application)
            return ResultDTOUtil.fail();
        return ResultDTOUtil.success(null);
    }

}
